﻿using System;
using System.Collections.Generic;
using EXT;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace BanHangCshap.DanhMuc.gkhtdl63.Tonghop
{
    public partial class frmLichSuGiaodich : Form
    {
        public frmLichSuGiaodich()
        {
            InitializeComponent();
        }

        DataTable xgDTKhachhang2 = new DataTable();
        DataTable xgDTNCC2 = new DataTable();

        private void Load_Khachang2()
        {
            Class.gkhtdl63.BLL.Khachhang2BLL xlKhachhang2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Khachhang2BLL();
            xgDTKhachhang2 = xlKhachhang2BLL.Khachhang2_Laydanhsach_View(1);
            if (xgDTKhachhang2 != null)
            {
                DataRow dr = xgDTKhachhang2.NewRow();
                dr[0] = DBNull.Value;
                dr[1] = DBNull.Value;
                dr[2] = DBNull.Value;
                dr[3] = DBNull.Value;
                xgDTKhachhang2.Rows.InsertAt(dr, 0);
                gleKhachhang2.Properties.DataSource = xgDTKhachhang2;
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Load_NCC2()
        {
            Class.gkhtdl63.BLL.Khachhang2BLL xlKhachhang2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Khachhang2BLL();
            xgDTNCC2 = xlKhachhang2BLL.Khachhang2_Laydanhsach_View(2);
            if (xgDTNCC2 != null)
            {
                DataRow dr1 = xgDTNCC2.NewRow();
                dr1[0] = DBNull.Value;
                dr1[1] = DBNull.Value;
                dr1[2] = DBNull.Value;
                dr1[3] = DBNull.Value;
                xgDTNCC2.Rows.InsertAt(dr1, 0);
                gleNCC.Properties.DataSource = xgDTNCC2;
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void frmLichSuGiaodich_Load(object sender, EventArgs e)
        {
            dtpTungay.EditValue = SDN.LayNgaydauthang();
            dtpDenngay.EditValue = DateTime.Now;
            Load_Khachang2();
            Load_NCC2();
        }

        DataTable xgDTGDKhach = new DataTable();

        Class.gkhtdl63.BLL.AllInOneBLL xgAllInOneBLL = new BanHangCshap.Class.gkhtdl63.BLL.AllInOneBLL();

        private void GDKhachHang()
        {
            long Id_Khachhang2 =(long)gleKhachhang2.EditValue.ToFloat();
            xgDTGDKhach = xgAllInOneBLL.Khachhang2_Intongtop(dtpTungay.EditValue, dtpDenngay.EditValue, Id_Khachhang2);
            DataColumn xlDC = new DataColumn("NoHientai", typeof(Int64));
            DataColumn xlDC1 = new DataColumn("LoaiGiaodich", typeof(string));
            DataRow[] xx = xgDTKhachhang2.Select(String.Format("Id = '{0}'", Id_Khachhang2));

            xgDTGDKhach.Columns.Add(xlDC);
            xgDTGDKhach.Columns.Add(xlDC1);

            DataRow dr = xgDTGDKhach.NewRow();
            dr[0] = DBNull.Value;
            dr[1] = "Công nợ ban đầu";
            dr[2] = xgAllInOneBLL.Phieuxuat2_TinhNocuKhachhangK(dtpTungay.EditValue, Id_Khachhang2).Rows[0][2]; ;//xx[0]["CongnoBandau"].ToInt64();
            dr[3] = 0;
            dr[4] = 1;
            dr[5] = 0;
            dr[6] = "";

            int x = xgDTGDKhach.Rows.Count;
            xgDTGDKhach.Rows.InsertAt(dr, x);

            Int64 Val = 0;
            for (int i = x; i >= 0; i--)
            {
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 1)
                {
                    Val += xgDTGDKhach.Rows[i]["Tongtien"].ToInt64() - xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                    if (!xgDTGDKhach.Rows[i][0].ToString().Trim().Equals(""))
                    {
                        xgDTGDKhach.Rows[i][6] = "Xuất hàng";
                    }
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 3)
                {
                    Val = Val - xgDTGDKhach.Rows[i]["Tongtien"].ToInt64() + xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                    xgDTGDKhach.Rows[i][6] = "Khách trả hàng";
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 5)
                {
                    Val = Val - xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDKhach.Rows[i][6] = "Phiếu thu";
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 6)
                {
                    Val = Val + xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDKhach.Rows[i][6] = "Phiếu chi";
                }
                xgDTGDKhach.Rows[i][5] = Val;
            }
            gcMain.DataSource = xgDTGDKhach;
        }

        DataTable xgDTGDNCC = new DataTable();
        private void GDNCC()
        {
            long Id_NCC2 = (long)gleNCC.EditValue.ToFloat();
            xgDTGDNCC = xgAllInOneBLL.Khachhang2_Intongtop(dtpTungay.EditValue, dtpDenngay.EditValue, Id_NCC2);
            DataColumn xlDC = new DataColumn("NoHientai", typeof(Int64));
            DataColumn xlDC1 = new DataColumn("LoaiGiaodich", typeof(string));
            DataRow[] xx = xgDTNCC2.Select(String.Format("Id = '{0}'", Id_NCC2));

            xgDTGDNCC.Columns.Add(xlDC);
            xgDTGDNCC.Columns.Add(xlDC1);

            DataRow dr = xgDTGDNCC.NewRow();
            dr[0] = DBNull.Value;
            dr[1] = "Công nợ ban đầu";
            dr[2] = xgAllInOneBLL.Phieuxuat2_TinhNocuKhachhangN(dtpTungay.EditValue,Id_NCC2).Rows[0][2];//xx[0]["CongnoBandau"].ToInt64();
            dr[3] = 0;
            dr[4] = 2;
            dr[5] = 0;
            dr[6] = "";

            int x = xgDTGDNCC.Rows.Count;
            xgDTGDNCC.Rows.InsertAt(dr, x);

            Int64 Val = 0;
            for (int i = x; i >= 0; i--)
            {
                if (xgDTGDNCC.Rows[i]["Loai"].ToInt64() == 2)
                {
                    Val += xgDTGDNCC.Rows[i]["Tongtien"].ToInt64() - xgDTGDNCC.Rows[i]["Thanhtoan"].ToInt64();
                    if (!xgDTGDNCC.Rows[i][0].ToString().Trim().Equals(""))
                    {
                        xgDTGDNCC.Rows[i][6] = "Nhập hàng";
                    }
                }
                if (xgDTGDNCC.Rows[i]["Loai"].ToInt64() == 4)
                {
                    Val = Val - xgDTGDNCC.Rows[i]["Tongtien"].ToInt64() + xgDTGDNCC.Rows[i]["Thanhtoan"].ToInt64();
                    xgDTGDNCC.Rows[i][6] = "Trả hàng NCC";
                }
                if (xgDTGDNCC.Rows[i]["Loai"].ToInt64() == 5)
                {
                    Val = Val + xgDTGDNCC.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDNCC.Rows[i][6] = "Phiếu thu";
                }
                if (xgDTGDNCC.Rows[i]["Loai"].ToInt64() == 6)
                {
                    Val = Val - xgDTGDNCC.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDNCC.Rows[i][6] = "Phiếu chi";
                }
                xgDTGDNCC.Rows[i][5] = Val;
            }
            gcSub.DataSource = xgDTGDNCC;
        }

        private void gleKhachhang2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index==1)
            {
                if (gleKhachhang2.EditValue != null)
                {
                    GDKhachHang();
                }
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void gleNCC_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                if (gleNCC.EditValue != null)
                {
                    GDNCC();
                }
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
